# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import models, api
from lxml import etree


class Production(models.Model):
    _inherit = 'mrp.production'

    @api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
        res = super(Production, self).fields_view_get(
            view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
        if view_type == "form" and not self.env.user.has_group("mrp_production_adjust.group_mrp_production_adjust"):
            doc = etree.XML(res['arch'])
            xpaths = [
                "//field[@name='track_number']",
                "//field[@name='mo_done_date']",
                "//field[@name='part_number']",
                "//field[@name='part_weight']",
                "//field[@name='date_planned_start']",
                "//field[@name='user_id']",
                "//field[@name='origin']",
                "//field[@name='order_plant']",
                "//field[@name='production_machine_num']",
                "//field[@name='product_attention']",
                "//field[@name='product_production_attribute_ids']",
                "//field[@name='attachment_ids']",
                "//field[@name='move_raw_ids']",
            ]
            for path in xpaths:
                for node in doc.xpath(path):
                    node.set('modifiers', '{"readonly": true}')
            action = self.env.ref("mrp.action_change_production_qty")
            xpaths = [
                "//button[@name=%d]" % action.id
            ]
            for path in xpaths:
                for node in doc.xpath(path):
                    node.set('modifiers', '{"invisible": true}')
            res['arch'] = etree.tostring(doc)
        return res
